package com.hxf.dao.order.impl;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.hxf.common.PublicUtil;
import com.hxf.dao.BaseDaoImpl;
import com.hxf.dao.order.OrderDao; 
import com.hxf.dto.MealOrderDetailDto;
import com.hxf.dto.MealOrderDto;
import com.hxf.dto.ReturnData;
import com.hxf.exception.DaoLevelException;
/**
 * DAO订单实现类
 * @author tengyicheng
 * @version [1.0, 2013/11/21]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
@SuppressWarnings("unchecked")
@Repository("orderDao")
public class OrderDaoImpl extends BaseDaoImpl implements OrderDao {

	@Override
	public ReturnData<MealOrderDto> findMealOrder(MealOrderDto orderDto,int targetPage, int pageSize) throws DaoLevelException {
		String sql = "";
		if (orderDto != null) {
			if (orderDto.getRestaurantId() != null) {
				sql += " and restaurantId = " + orderDto.getRestaurantId();
			}
			if (orderDto.getUserId() != null) {
				sql += " and userId = " + orderDto.getUserId();
			}
			
			if(PublicUtil.isNotNullAndEmpty(orderDto.getContact())){
				sql += " and contact = '%"+orderDto.getContact().trim()+"%'" ;
			}
		}
		 
		return super.findBy("from MealOrderDto where 1=1" + sql , targetPage, pageSize);
	}

	@Override
	public ReturnData<MealOrderDetailDto> findMealOrderDetail(MealOrderDetailDto orderDetailDto, int targetPage, int pageSize) throws DaoLevelException { 
		String sql = "";
		if (orderDetailDto != null && orderDetailDto.getOrderId() != null) {
			sql = " where orderId = " + orderDetailDto.getOrderId();
		}
		return super.findBy("from MealOrderDetailDto" + sql , targetPage, pageSize); 
	}

	@Override
	public List<MealOrderDetailDto> findMealOrderDetail(MealOrderDetailDto orderDetailDto) throws DaoLevelException {
		String sql = "";
		if (orderDetailDto != null && orderDetailDto.getOrderId() != null) {
			if(PublicUtil.isNotNullAndEmpty(orderDetailDto.getOrderId())){
				sql = " and orderId =  '"+orderDetailDto.getOrderId()+"' ";
			}
		}
		return super.findBy("from MealOrderDetailDto where 1=1 " + sql); 
	}

	@Override
	public int deteleMealOrderDetailByOrderId(String orderId) throws DaoLevelException {
		return super.executeUpdateBySql("delete from rk_meal_order_detail where order_id='"+orderId+"'");
	}
	
	@Override
	public int deleteMealOrderBy(String orderId) throws DaoLevelException{
		return super.executeUpdateBySql(" delete from rk_meal_order  where order_id = '"+orderId+"'");
	}
}
